package com.atguigu.service.impl;

import com.atguigu.dao.DeliveryDao;
import com.atguigu.dao.impl.DeliveryDaoImpl;
import com.atguigu.pojo.Company;
import com.atguigu.pojo.Delivery;
import com.atguigu.service.CompanyService;
import com.atguigu.service.DeliveryService;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeliveryServiceImpl implements DeliveryService {
    private DeliveryDao deliveryDao = new DeliveryDaoImpl();
    private CompanyService companyService = new CompanyServiceImpl();

    @Override
    public List<Delivery> findAllDeliveryByUserId(Integer userId) throws Exception {
        List<Delivery> allDeliveryByUserId = deliveryDao.findAllDeliveryByUserId(userId);
        List<Company> allCompany = companyService.findAllCompany();
        HashMap<Integer, Company> integerCompanyHashMap = new HashMap<>();
        allCompany.forEach(company -> {
            integerCompanyHashMap.put(company.getId(), company);
        });
        //将map中对应的快递站序号进行提取 获取value 并且传递给delivery中的javaBean的company值中
        allDeliveryByUserId.forEach(delivery -> {
            delivery.setCompany(integerCompanyHashMap.get(delivery.getCompanyId()));
        });
        return allDeliveryByUserId;
    }

    @Override
    public void addDelivery(Integer uid, Map<String, String[]> parameterMap) throws SQLException {
        deliveryDao.addDelivery(uid,parameterMap);
    }
}
